package com.microsec.iot.demo;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;

import com.microsec.iot.demo.PostAsynCommandV4;
import com.microsec.iot.utils.Constant;
import com.microsec.iot.utils.IotHttpUtil;

@SuppressWarnings("unused")
@WebServlet("/CommandDown")
public class CommandDown extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public CommandDown() {
        super();
    }

    /*Method: POST
    request: https://server:port/iocm/app/cmd/v1.4.0/deviceCommands
    Header:
    "app_key: ******"
    "Authorization:Bearer******"
    Content-Type:application/json
    Body:
    {
    "deviceId": "********",
    "command": {
    "serviceId": "********",
    "method": "********",
    "paras": {
    "paraName1": "paraValue1",
    "paraName2": "paraValue2"
    }
    },
    "callbackUrl": "http://127.0.0.1/cmd/callbackUrl"，
    "maxRetransmit":******,
    }*/
    
    /*response:
    	Status Code: 201 OK
    	Content-Type:application/json
    	Body:
    	{
    	"commandId": "********",
    	"appId": "********",
    	"deviceId": "********",
    	"command": {
    	"serviceId": "********",
    	148
    	"method": "********",
    	"paras": {
    	"paraName1": "paraValue1",
    	"paraName2": "paraValue2"
    	}
    	},
    	"callbackUrl": "http://127.0.0.1/cmd/callbackUr",
    	"expireTime": null,
    	"status": "PENDDING",
    	"result": null,
    	"creationTime": "20170222T164000Z",
    	"executeTime": null,
    	"platformIssuedTime": null,
    	"deliveredTime": null,
    	"issuedTimes": null,
    	"maxRetransmit":******
    	}*/
    /*public DeviceServiceInvocationOutDTO invocateDeviceService(DeviceServiceInvocationInDTO dsiid, String appId, String accessToken)
    		throws NorthApiException{}*/
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		String deviceId = "uyqwuqwyheqe";
		String callbackUrl = Constant.CALLBACK_BASE_URL;
		String serviceId = "qwdqwdqwdqw";
		String method = type;
		String requestId = UUID.randomUUID().toString();
		String jsonStr = "{\"type\":"+type+",\"requestId\":"+requestId.replaceAll("-", "")+"\"}";
		String resp = PostAsynCommandV4.getCommandHttpResponseBody(deviceId, callbackUrl, serviceId, method, jsonStr);
		System.out.println(resp);
		// HttpResponse responseData = IotHttpUtil.sendRequest(url, jsonRequest);
	}
	
	protected static void ttt() throws ServletException, IOException {
		String type = "type";
		String deviceId = "uyqwuqwyheqe";
		String callbackUrl = Constant.CALLBACK_BASE_URL;
		String serviceId = "qwdqwdqwdqw";
		String method = type;
		String requestId = UUID.randomUUID().toString();
		String jsonStr = "{\"commandType\":"+type+",\"requestId\":"+requestId.replaceAll("-", "")+"\"}";
		String resp = PostAsynCommandV4.getCommandHttpResponseBody(deviceId, callbackUrl, serviceId, method, jsonStr);
		System.out.println(resp);
		// HttpResponse responseData = IotHttpUtil.sendRequest(url, jsonRequest);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	public static void main(String[] ss) {
		try {
			ttt();
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
